package com.xuyiwei.mayi.topic_demo;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created by xuyiwei on 2019/4/22.
 */
public class Producter {
    public static void main(String[] args) throws JMSException {
        // ConnectionFactory ：连接工厂，JMS 用它创建连接
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://127.0.0.1:61616");
        //创建jms连接
        Connection connection = connectionFactory.createConnection();
        connection.start();
        //创建会话 第一个是是否支持事务  第二个参数是事务的级别
        Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
        //创建队列
        //创建生产者
        MessageProducer producter =session.createProducer(null);
        // 设置不持久化
        producter.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        for(int i=0;i<10;i++){
            send(session,producter,i);
        }
        connection.close();
    }

    private static void send(Session session, MessageProducer producter,int i) throws JMSException {
        TextMessage message = session.createTextMessage("这是消息"+i);
        Destination destination = session.createTopic("mayi_topic");
        producter.send(destination,message);
        session.commit();
    }
}
